<?php
class Notification_Bd{

  /* Initialisation d'un objet Notification à partir d'un id_notification*/ 
	public static function getNotification($id) {  
	 	$db = Outils_Bd::getInstance()->getConnexion();
		$sth=$db->prepare("SELECT * FROM Notification WHERE id_notification=:id");
		$data=array('id' => $id);
		$sth->execute($data);
		$ligne = $sth->fetch(PDO::FETCH_ASSOC);
  
		/*retourner un objet Title */
		return Notification::initialize($ligne);
  	}
	
	/* Méthode getNotificationsByType($type,$id_object,$state) : Retourne une liste de notifications selon son type */
	public static function getNotificationsByType($type,$id_user_object,$state=NOTIFICATION_NEW){
		$db = Outils_Bd::getInstance()->getConnexion();
		$sth=$db->prepare("SELECT * FROM Notification WHERE notification_type=:type AND id_user_object=:id_user_object AND state=:state ORDER BY time DESC");
		$data = array('type' => $type,
						'id_user_object' => $id_user_object,
						'state' => $state
						);
		$sth->execute($data);
		$liste = array();
		while (($ligne = $sth->fetch(PDO::FETCH_ASSOC)) !== false) {
		  	// ajouter un objet Title dans le tableau
		  	$liste[] = Notification::initialize($ligne);
		}
		return $liste;
	}
	
		/* Méthode getNotifications($type) : Retourne une liste de notifications */
	public static function getNotifications($clause,$data=array()){
		$db = Outils_Bd::getInstance()->getConnexion();
		$requete = "SELECT * FROM Notification WHERE {$clause}";
		if (isset($data['offset'])) {
			$requete .=	" LIMIT {$data['offset']}, {$data['itemsPerPage']}";
			unset($data['offset']);
			unset($data['itemsPerPage']);
		}
		$sth=$db->prepare($requete);
		$sth->execute();
		$liste = array();
		while (($ligne = $sth->fetch(PDO::FETCH_ASSOC)) !== false) {
		  	// ajouter un objet Title dans le tableau
		  	$liste[] = Notification::initialize($ligne);
		}
		return $liste;
	}
	
		// Methode count
    public static function count($clause="1=1", $data=array()) {
		
	  	$db = Outils_Bd::getInstance()->getConnexion();
	  	$sth=$db->prepare("SELECT count(*) as totalItems FROM Notification WHERE {$clause}");
	  	$sth->execute($data);
	  	$ligne = $sth->fetch(PDO::FETCH_ASSOC);
	 	return $ligne['totalItems'];
  }
	
		// Méthode addNotification($notification) : ajoute une notification dans la table Notification
	static public function addNotification($notification) { 
 
		$db = Outils_Bd::getInstance()->getConnexion();
		$sth=$db->prepare("INSERT INTO Notification ( id_notification, id_user_object, id_user_subject, notification_type, state, time ) VALUES ( :id_notification, :id_user_object, :id_user_subject, :notification_type, :state, :time )");
		$sth2=$db->prepare("SELECT * FROM Notification WHERE id_notification=LAST_INSERT_ID()");
		$data=array(
			'id_notification' => $notification->getIdNotification(),
			'id_user_object' => $notification->getIdUserObject(),
			'id_user_subject' => $notification->getIdUserSubject(),
			'notification_type' => $notification->getNotificationType(),
			'state' => $notification->getState(),
			'time' => $notification->getTime()
			);
	
		$sth->execute($data);
		$sth2->execute();
		$ligne = $sth2->fetch(PDO::FETCH_ASSOC);
		return Notification::initialize($ligne);
	}
	
	  // Méthode modifyNotification($notification) : modifie une évaluation donnée
   static public function modifyNotification($notification) { 
  
		$db = Outils_Bd::getInstance()->getConnexion();
		$sth=$db->prepare("UPDATE Notification SET state=:state WHERE id_notification=:id_notification ");
		$data=array(
			'id_notification' => $notification->getIdNotification(),
			'state' => $notification->getState()
		);

		$sth->execute($data);
	}
	
	// Méthode getNotificationByRelation($id_user_subject,$id_user_object,$type) : Renvoit la Notification concerné sur l'id_user sujet, l'id_user objet
	// et le type de notification. Si aucun résultat, renvoit false;
	public static function getNotificationByRelation($id_user_subject,$id_user_object,$type){
	  	$db = Outils_Bd::getInstance()->getConnexion();
	  	$sth=$db->prepare("SELECT * FROM Notification WHERE notification_type=:type AND id_user_object=:id_user_object AND id_user_subject=:id_user_subject");
		$data = array('type' => $type,
						'id_user_object' => $id_user_object,
						'id_user_subject' => $id_user_subject
						);
		$sth->execute($data);
	  	$ligne = $sth->fetch(PDO::FETCH_ASSOC);
	 	if (! $ligne)
			return false;
		else
			return Notification::initialize($ligne);
	}		
	
	
	
}

?>